package com.xiaoge.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaoge.entity.CourseCategory;
import com.xiaoge.mapper.CourseCategoryMapper;
import com.xiaoge.service.CourseCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoge.utils.CourseTreeBuilder;
import com.xiaoge.utils.MenuTreeBuilder;
import com.xiaoge.vo.CourseCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程分类 服务实现类
 * </p>
 *
 * @author XiaoGe
 * @since 2022-10-28
 */
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

//    @Override
//    public List<CourseCategory> findMenuTree() {
//
//        List<CourseCategory> menus = this.baseMapper.selectList(null);
//        //菜单树的构造工具类
//        List<CourseCategory> menusList = CourseTreeBuilder.build(menus);
//        return menusList;
//    }

    @Override
    public List<CourseCategory> findMenuTree(String categoryName) {

        List<CourseCategory> menus = this.baseMapper.selectListByCategoryName(categoryName);
        //菜单树的构造工具类
        List<CourseCategory> menusList = CourseTreeBuilder.build(menus);
        return menusList;
    }


    @Override
    public CourseCategory editCategory(String id) {

        return this.baseMapper.selectById(id);
    }

    @Override
    public int updateCategory(CourseCategoryVO courseCategoryVO) {
        CourseCategory category = new CourseCategory();
        //浅拷贝
        BeanUtils.copyProperties(courseCategoryVO,category);
        return this.baseMapper.updateById(category);
    }

    @Override
    public int addNav(CourseCategoryVO courseCategoryVO) {
        CourseCategory category = new CourseCategory();
        BeanUtils.copyProperties(courseCategoryVO,category);
        category.setParentId(0l);
        category.setFloor(1);
        category.setStatusId(1);
        return this.baseMapper.insert(category);
    }

    @Override
    public List<CourseCategory> getCourseCategoryListLike(String categoryName) {

        List<CourseCategory> list =  this.baseMapper.getCourseCategoryListLike(categoryName);

        return list;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        UpdateWrapper<CourseCategory> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.eq("id",id);
        Integer newStatus = (status==1?0:1);
        categoryUpdateWrapper.set("status",newStatus);
        int update = this.baseMapper.update(null, categoryUpdateWrapper);
        return update;
    }
}
